package com.jinlia.show.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.model.po.DeliveryOrderPO;
import com.jinlia.show.modules.base.model.query.DeliveryOrderQuery;
import com.jinlia.show.modules.base.model.vo.DeliveryOrderVO;
import com.jinlia.show.modules.base.system.entity.DeliveryOrder;
import com.jinlia.show.modules.base.system.entity.Order;
import com.jinlia.show.modules.base.system.service.DeliveryOrderMapperService;
import com.jinlia.show.modules.base.system.service.OrderLogMapperService;
import com.jinlia.show.modules.system.service.DeliveryOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
 * 配送订单
 *
 * @author JinLia
 * @date 2020/03/09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/deliveryOrder")
public class DeliveryOrderController {

    @Autowired
    DeliveryOrderService deliveryOrderService;
    @Autowired
    OrderLogMapperService orderLogMapperService;

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<DeliveryOrderPO> getById(@PathVariable("id") Long id) {
        DeliveryOrderPO details = deliveryOrderService.getDetails(id);
        return ResponseEntity.ok(details);
    }
    @GetMapping("/byOrder/{id}")
    public ResponseEntity<DeliveryOrderPO> getByOrderId(@PathVariable("id") Long id) {
        DeliveryOrderPO details = deliveryOrderService.getByOrderId(id);
        return ResponseEntity.ok(details);
    }
    /**
     * 选择配送方式
     */
    @PostMapping("/confirm")
    public ResponseEntity<Boolean> confirmDeliveryType(@RequestBody  DeliveryOrder deliveryOrder) {
        return ResponseEntity.ok(deliveryOrderService.confirmDelivery(deliveryOrder));
    }
    /**
     * 配送单查询
     *
     * @param queryParams
     * @return
     */
    @PostMapping("/page")
    public ResponseEntity<Page<DeliveryOrderVO>> getPage(@RequestBody DeliveryOrderQuery queryParams) {
        Page<DeliveryOrderVO> pagePO = deliveryOrderService.getPagePO(queryParams);
        return ResponseEntity.ok(pagePO);
    }



    /**
     * 商家动作:
     * 配送完成(配送员确认)
     */
    @PostMapping("ok")
    public Boolean okDeliveryOrder(@RequestBody DeliveryOrder deliveryOrder) {
        deliveryOrderService.okDeliveryOrder(deliveryOrder);
        //订单操作记录
        orderLogMapperService.addOrderLog("PS", deliveryOrder.getId(), "商户送达", "待顾客确认");
        return Boolean.TRUE;
    }
}
